package com.compmov.socialnetwork;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class Lugar extends Activity {
    /** Called when the activity is first created. */
	private TextView txtNombre;
	private TextView txtLugar;
	private TextView txtDescripcion;
	private TextView txtRanking;
	private ImageView imgFoto;
	private ImageView imgRanking;
	
	String nombre;
    String descripcion;
    String ranking="0";
    String url;
    String address;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lugar);
        Bundle extras=getIntent().getExtras();
        nombre=extras.getString("nombre");
        descripcion=extras.getString("descripcion");
        ranking=extras.getString("ranking");
        url=extras.getString("url");
        address=extras.getString("address");
        this.txtNombre =(TextView)findViewById(R.id.nombre_lugar);
        this.txtLugar =(TextView)findViewById(R.id.ubicacion);
        this.txtDescripcion =(TextView)findViewById(R.id.descripcion);
      //  this.txtRanking =(TextView)findViewById(R.id.ranking);
        this.imgFoto = (ImageView)findViewById(R.id.ImageLugar);
        this.imgRanking = (ImageView)findViewById(R.id.lugranking);  
        Drawable image = ImageOperations(getApplication(),url,"image.jpg");			
		imgFoto.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		imgFoto.setImageDrawable(image);
		
		txtNombre.setText(nombre);
		txtDescripcion.setText("Descripción\n"+descripcion);
		//txtRanking.setText("Ranking: "+ranking);
		txtLugar.setText(address);
		setRankingImage();
		
        
    }
    
    public void setRankingImage(){
    	int rank=Integer.valueOf(ranking);
        switch(rank){
        case 1: imgRanking.setImageResource(R.drawable.ranking1); break;
        case 2: imgRanking.setImageResource(R.drawable.ranking2); break;
        case 3: imgRanking.setImageResource(R.drawable.ranking3); break;
        case 4: imgRanking.setImageResource(R.drawable.ranking4); break;
        default: imgRanking.setImageResource(R.drawable.ranking5); 
        }
    }
    
    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    	try {
    		InputStream is = (InputStream) this.fetch(url);
    		Drawable d = Drawable.createFromStream(is, "src");
    		return d;
    	} catch (MalformedURLException e) {
    		e.printStackTrace();
    		return null;
    	} catch (IOException e) {
    		e.printStackTrace();
    		return null;
    	}
    }

    public Object fetch(String address) throws MalformedURLException,IOException {
    	URL url = new URL(address);
    	Object content = url.getContent();
    	return content;
    }

}
